package com.sdy.kzyy.web.controller;

import com.sdy.kzyy.biz.constants.RedisConstants;
import com.sdy.kzyy.biz.model.User;
import com.sdy.kzyy.biz.service.KeyValueConfigurationsService;
import com.sdy.kzyy.biz.service.UserService;
import com.sdy.kzyy.biz.utils.TimeChecker;
import com.sdy.kzyy.web.config.WxConfig;
import com.sdy.mvc.controller.BaseController;
import com.sdy.common.model.Response;
import com.sdy.common.utils.EncodeUtil;
import com.sdy.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.security.KeyPair;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zzq on 2019/1/10.
 */
@Slf4j
@RestController
@RequestMapping("/index")
@Api(tags = "基础信息接口")
public class IndexController extends BaseController {
    @Autowired
    private RedisService redisService;
    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private UserService userService;
    @Autowired
    private KeyValueConfigurationsService keyValueConfigurationsService;

    @GetMapping("/publicKey")
    public Response publicKey(HttpServletRequest request) throws Exception {
        KeyPair keyPair = EncodeUtil.rsaGenKeyPair();
        String publicKeyStr = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()));
        redisService.set(RedisConstants.REDIS_PREFIX + "privateKey_" + getUserId(request), Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()));
        return Response.success(publicKeyStr);
    }

    @GetMapping("/oaAppId")
    @ApiOperation(value = "获取公众号APPID")
    public Response oaAppId() {
        return Response.success(wxConfig.getOaAppid());
    }

    @GetMapping("/userInfo")
    public Response userInfo(HttpServletRequest request) {
        Integer userId = getUserId(request);
        if(userId == null) {
            return Response.error("");
        }
        User user = userService.getById(userId);
        if(user == null) {
            return Response.error("");
        }
        return Response.success(user.getId());
    }

    @GetMapping("/information")
    @ApiOperation(value = "首页数字型数据")
    public Response information() {
        Map<String, String> map = new HashMap<>();
        map.put("appointment", keyValueConfigurationsService.getAppointmentTime());
        // map.put("extract", keyValueConfigurationsService.getExtractTime());
        map.put("limit", keyValueConfigurationsService.getLimitationCount());
        map.put("interval", keyValueConfigurationsService.getOrderInitiationInterval());
        map.put("duedate", keyValueConfigurationsService.getOverdueDate());
        map.put("appointmentWeek", keyValueConfigurationsService.getAppointmentWeek());
        return Response.success(map);
    }

    @GetMapping("/off")
    @ApiOperation(value = "是否开启口罩预约")
    public Response off() {
        if(TimeChecker.check(keyValueConfigurationsService.getAppointmentTime(),
                keyValueConfigurationsService.getAppointmentWeek())) {
            return Response.success();
        }else {
            return Response.error("不在可预约时间范围内");
        }
    }

}
